﻿using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;
using System.Xml;

namespace DemoGame1
{
    public class MediaControler
    {
        public struct PLAYER
        {
            public static int attack = 0;
            public static int damaged = 1;
            public static int dead = 2;
            public static int skill = 3;
        }

        public struct ENEMY
        {
            public static int sword = 4;
            public static int punch = 5;
            public static int damaged = 6;
            public static int skill = 7;
            public static int dead = 8;
        }


        ContentManager _content;
        List<SoundEffect> soundEffectPlayer;
        SoundEffectInstance[] soundEffectInstancePlayer;

        List<SoundEffect> soundEffectEnemy;
        SoundEffectInstance[] soundEffectInstanceEnemy;

        Song _musicTheme;
        float volume;
        float volumeFX;

        public int currentSong = -1;

        public float VolumeFX
        {
            get { return volumeFX; }
            set { volumeFX = value; }
        }

        public float Volume
        {
            get { return volume; }
            set { volume = value; }
        }

        string[] songname;

        public MediaControler(ContentManager content)
        {
            _content = content;
            XmlDocument doc = new XmlDocument();
            doc.Load(XMLDataProvider.strMedia);
            volume = 1.0f;
            volumeFX = 1.0f;
            XmlElement songs = (XmlElement)doc.SelectSingleNode("//MusicTheme");
            songname = new string[songs.ChildNodes.Count];
            {
                int index = 0;
                foreach (XmlElement elm in songs.ChildNodes)
                {
                    if (elm.Name == "Song")
                    {
                        songname[index] = elm.InnerText.Trim();
                        index++;
                    }
                }
            }
            XmlElement Player = (XmlElement)doc.SelectSingleNode("//Player");

            AddSoundEffects(ref soundEffectPlayer, Player);
            soundEffectInstancePlayer = new SoundEffectInstance[soundEffectPlayer.Count];

            for (int i = 0; i < soundEffectPlayer.Count; i++)
            {
                soundEffectInstancePlayer[i] = soundEffectPlayer[i].CreateInstance();
            }

            XmlElement enemy = (XmlElement)doc.SelectSingleNode("//Enemy");

            AddSoundEffects(ref soundEffectEnemy, enemy);
            soundEffectInstanceEnemy = new SoundEffectInstance[soundEffectEnemy.Count];

            for (int i = 0; i < soundEffectEnemy.Count; i++)
            {
                soundEffectInstanceEnemy[i] = soundEffectEnemy[i].CreateInstance();
            }

        }

        private void AddSoundEffects(ref List<SoundEffect> listSound, XmlElement SFXs)
        {
            listSound = new List<SoundEffect>();
            {
                foreach (XmlElement elm in SFXs.ChildNodes)
                {
                    if (elm.Name == "SoundEffect")
                    {
                        string sfx = elm.InnerText.Trim();
                        SoundEffect temp = _content.Load<SoundEffect>(sfx);
                        listSound.Add(temp);
                    }
                }
            }
        }

        public void LoadMusicTheme(string songname)
        {
            StopMusicTheme();
            _musicTheme = _content.Load<Song>(songname);
        }

        public void LoadMusicTheme(int numofsong)
        {
            if (numofsong != currentSong)
            {
                currentSong = numofsong;
                StopMusicTheme();
                _musicTheme = _content.Load<Song>(songname[numofsong]);
            }
        }

        public void Update(GameTime gameTime)
        {
            MediaPlayer.Volume = volume;
            for (int i = 0; i < soundEffectInstancePlayer.Length; i++)
            {
                soundEffectInstancePlayer[i].Volume = volumeFX;
            }
            for (int i = 0; i < soundEffectInstanceEnemy.Length; i++)
            {
                soundEffectInstanceEnemy[i].Volume = volumeFX;
            }
        }

        int damage = 11;
        int attack = 0;
        public void PlaySoundEffect(int i)
        {
            switch (i)
            {
                //player attack
                case 0:
                    {
                        soundEffectInstancePlayer[attack].Play();
                        attack = (attack + 1) % 11;
                    }
                    break;
                //player damage
                case 1:
                    {
                        soundEffectInstancePlayer[damage].Play();
                        damage = damage + 1;
                        if (damage == 14)
                        {
                            damage = 11;
                        }
                    }
                    break;
                //player dead
                case 2:
                    {
                        soundEffectInstancePlayer[14].Play();
                    }
                    break;
                //player skill
                case 3:
                    //ryu
                    if (Playscreen.CHARACTER == 0)
                    {
                        soundEffectInstancePlayer[15].Play();
                    }
                    //ken
                    if (Playscreen.CHARACTER == 1)
                    {
                        soundEffectInstancePlayer[16].Play();
                    }
                    break;
                //Enemi sword 
                case 4:
                    {
                        soundEffectInstanceEnemy[0].Play();
                    }
                    break;
                //Enemi punch 
                case 5:
                    {
                        soundEffectInstanceEnemy[1].Play();
                    }
                    break;
                //Enemi damage 
                case 6:
                    {
                        soundEffectInstanceEnemy[2].Play();
                    }
                    break;
                //Enemi skill 
                case 7:
                    {
                        soundEffectInstanceEnemy[3].Play();
                    }
                    break;
                //Enemi dead 
                case 8:
                    {
                        soundEffectInstanceEnemy[4].Play();
                    }
                    break;
                default:
                    break;

            }

        }

        public void PlayMusicTheme()
        {
            if (MediaPlayer.State != MediaState.Playing)
                MediaPlayer.Play(_musicTheme);
        }

        public void PauseMusicTheme()
        {
            if (MediaPlayer.State == MediaState.Playing)
                MediaPlayer.Pause();
        }

        public void ResumeMusicTheme()
        {
            if (MediaPlayer.State == MediaState.Paused)
                MediaPlayer.Resume();

        }
        public void StopMusicTheme()
        {
            MediaPlayer.Stop();
        }

        public string MusicPlayerState()
        {
            return MediaPlayer.State.ToString();
        }
    }
}
